package com.mohammad.fun.dtpkrisgethin;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubeStandalonePlayer;

public class MainActivity extends Activity {

	// VideoView vv;
	private static final int REQ_START_STANDALONE_PLAYER = 1;
	private static final int REQ_RESOLVE_SERVICE_MISSING = 2;

	private static final String VIDEO_ID = "7iZcGBrun5Y";

	//Intent intent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// vv = (VideoView) this.findViewById(R.id.VideoView);
		// Uri videoUri = Uri
		// .parse("rtsp://v7.cache2.c.youtube.com/CjYLENy73wIaLQmWn-4aGFwm7hMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYIfAybXjhcf3UAw=/0/0/0/video.3gp");
		// vv.setMediaController(new MediaController(this));
		// vv.setVideoURI(videoUri);
		// //vv.start();

		// Getting the URL to work

		// String video_path = "http://www.youtube.com/watch?v=7iZcGBrun5Y";
		// Uri uri = Uri.parse(video_path);

		// With this line the youtube application, if installed, will launch
		// immediately.
		// Without it you will be prompted with a list of the application to
		// choose.
		// uri = Uri.parse("vnd.youtube:" + uri.getQueryParameter("v"));
		//
		// intent = new Intent(Intent.ACTION_VIEW, uri);
		// intent.putExtra("force_fullscreen",true);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// MenuInflater inflater = getMenuInflater();
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	public void btn_watchVideo(View v) {

		Intent intent = null;

		intent = YouTubeStandalonePlayer.createVideoIntent(this,
				DeveloperKey.DEVELOPER_KEY, VIDEO_ID, 0, true, true);

		if (intent != null) {
			if (canResolveIntent(intent)) {
				startActivityForResult(intent, REQ_START_STANDALONE_PLAYER);
			} else {
				// Could not resolve the intent - must need to install or update
				// the YouTube API service.
				YouTubeInitializationResult.SERVICE_MISSING.getErrorDialog(
						this, REQ_RESOLVE_SERVICE_MISSING).show();
			}
		}
		// vv.start();
		//startActivity(intent);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == REQ_START_STANDALONE_PLAYER
				&& resultCode != RESULT_OK) {
			YouTubeInitializationResult errorReason = YouTubeStandalonePlayer
					.getReturnedInitializationResult(data);
			if (errorReason.isUserRecoverableError()) {
				errorReason.getErrorDialog(this, 0).show();
			} else {
				String errorMessage = String.format(
						getString(R.string.error_player),
						errorReason.toString());
				Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
			}
		}
	}

	private boolean canResolveIntent(Intent intent) {
		List<ResolveInfo> resolveInfo = getPackageManager()
				.queryIntentActivities(intent, 0);
		return resolveInfo != null && !resolveInfo.isEmpty();
	}

//	private int parseInt(String text, int defaultValue) {
//		if (!TextUtils.isEmpty(text)) {
//			try {
//				return Integer.parseInt(text);
//			} catch (NumberFormatException e) {
//				// fall through
//			}
//		}
//		return defaultValue;
//	}

	public void leg(View v) {
		Log.d("Mohammad Button", "Leg Clicked");

		startActivity(new Intent(this, leg.class));
	}

	public void chest(View v) {
		Log.d("Mohammad Button", "Chest Clicked");

		startActivity(new Intent(this, chest.class));
	}

	public void arm(View v) {
		Log.d("Mohammad Button", "Arm Clicked");

		startActivity(new Intent(this, arm.class));
	}

	public void shoulder(View v) {
		Log.d("Mohammad Button", "Shoulder Clicked");

		startActivity(new Intent(this, shoulder.class));
	}

	public void btn_sendfeedback(View v) {
		Log.d("Mohammad Button", "Feedback send Clicked");

		startActivity(new Intent(this, NewProductActivity.class));
	}

}